# -*- coding: utf8 -*-
from flask import Flask
from flask_restful import Api
from flask import request
from beidou_code_point import beidou_grid_code_point
from beidou_code_line import beidou_grid_code_line
from beidou_code_polygon import beidou_grid_code_polygon
from web_resource import BeiDouGridCode2DimensionPoint
from web_resource import BeiDouGridCode2DimensionLine
from web_resource import BeiDouGridCode2DimensionPolygon
from time import time


app = Flask(__name__)


# 直接方式
@app.route('/beidou/grid/2dimension/point')
def bediou_grid_code_point_view():
    time1 = time()
    args = request.args
    # print(args)
    longitude = args.get('longitude')
    latitude = args.get('latitude')
    level_n = int(args.get('level'))
    result = beidou_grid_code_point(longitude, latitude, level_n)
    print('point cost time is {}'.format(time()-time1))
    return result

@app.route('/beidou/grid/2dimension/line')
def bediou_grid_code_line_view():
    args = request.args
    # print(args)
    longitudeList = args.get('longitudeList')
    latitudeList = args.get('latitudeList')
    level_n = int(args.get('level'))
    result = beidou_grid_code_line(longitudeList, latitudeList, level_n)
    return result

@app.route('/beidou/grid/2dimension/polygon')
def bediou_grid_code_polygon_view():
    args = request.args
    # print(args)
    longitudeList = args.get('longitudeList')
    latitudeList = args.get('latitudeList')
    level_n = int(args.get('level'))
    result = beidou_grid_code_polygon(longitudeList, latitudeList, level_n)
    return result


# # 借助于框架
# restful_app = Api(app=app)
# restful_app.add_resource(BeiDouGridCode2DimensionPoint, '/beidou/grid/2dimension/point')
# restful_app.add_resource(BeiDouGridCode2DimensionLine, '/beidou/grid/2dimension/line')
# restful_app.add_resource(BeiDouGridCode2DimensionPolygon, '/beidou/grid/2dimension/polygon')


if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5000, debug=False)

